//
//  BaseViewController+PopSheet.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/15.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "BaseViewController+PopSheet.h"
#import <UMSocialCore/UMSocialCore.h>
@implementation BaseViewController (PopSheet)
// popSheet

- (SnailSheetView *)sheetViewWithDelegate:(id<SnailSheetViewConfigDelegate>)delegate {
    
    SnailSheetView * sheet = [[SnailSheetView alloc] initWithFrame:CGRectMake(100, 100, SCREEN_WIDTH, 300) configDelegate:delegate];
   // sheet.headerLabel.text = @"IMGuider.com";
    
    sheet.models = [self sheetModels];
    [sheet autoresizingFlexibleHeight];
    
    
    return sheet;
}

- (void)popSheet {
    
    SnailSheetView * sheet = [self sheetViewWithDelegate:self];
    sheet.delegate = self;
    @weakify(self);
    sheet.didClickFooter = ^(SnailSheetView * _Nonnull sheetView) {
        @strongify(self);
        [self.sl_popupController dismiss];
    };
    
    
    self.sl_popupController = [SnailPopupController new];
    self.sl_popupController.layoutType = PopupLayoutTypeBottom;
    [self.sl_popupController presentContentView:sheet];
    
}


#pragma mark - SnailSheetViewConfig

- (SnailSheetViewLayout *)layoutOfItemInSheetView:(SnailSheetView *)sheetView {
    
    return [SnailSheetViewLayout layoutWithItemSize:CGSizeMake(SCREEN_WIDTH/4, 100)
                                      itemEdgeInset:UIEdgeInsetsMake(20, 0,0, 0)
                                        itemSpacing:0
                                     imageViewWidth:50
                                         subSpacing:10];
}


#define titleKey @"title"
#define imgNameKey @"imageName"
#define PlatformType @"platformType"

- (NSArray *)sheetModels {
    
    NSArray *arr1 = @[@{titleKey   : @"SHARE_WECHAT",
                        imgNameKey : @"微信",
                        PlatformType:@(UMSocialPlatformType_WechatSession)},
                      
                      @{titleKey   : @"SHARE_MOMENTS",
                        imgNameKey : @"朋友圈",
                        PlatformType:@(UMSocialPlatformType_WechatTimeLine)},
                      
                      @{titleKey   : @"SHARE_QQ",
                        imgNameKey : @"QQ",
                        PlatformType:@(UMSocialPlatformType_QQ)},
                      
                      @{titleKey   : @"SHARE_QZONE",
                        imgNameKey : @"QQ空间",
                        PlatformType:@(UMSocialPlatformType_Qzone)},
                      
                      @{titleKey   : @"SHARE_WEIBO",
                        imgNameKey : @"微博",
                        PlatformType:@(UMSocialPlatformType_Sina)},
                      
                      @{titleKey   : @"SHARE_FACEBOOK",
                        imgNameKey : @"Facebook",
                        PlatformType:@(UMSocialPlatformType_Facebook)}
                      ];

//                      @{titleKey   : @"SHARE_TWITTER",
//                        imgNameKey : @"Twitter",
//                        PlatformType:@(UMSocialPlatformType_Twitter)}
//                      ]
    
    NSMutableArray *array1 = [NSMutableArray array];
    for (NSDictionary *dict in arr1) {
        NSString *text = [dict objectForKey:titleKey];
        NSString *imgName = [dict objectForKey:imgNameKey];
        NSInteger type = ((NSNumber *)[dict objectForKey:PlatformType]).integerValue;
        if ([[UMSocialManager defaultManager] isInstall:type] || type == UMSocialPlatformType_Sina || type == UMSocialPlatformType_Facebook) {
            
            [array1 addObject:[SnailSheetItemModel modelWithText:text
                                                           image:[UIImage imageNamed:imgName] platformType:type]];
            
        }
        
    }
    
    NSMutableArray *array2 = [NSMutableArray array];
    
    if (array1.count > 4) {
        
        array2 =[NSMutableArray arrayWithArray:[array1 subarrayWithRange:NSMakeRange(4, array1.count - 4)]];
        [array1 removeObjectsInArray:array2];
    }
    
    return [NSMutableArray arrayWithObjects:array1, array2, nil];
}

- (void)sheetView:(nonnull SnailSheetView *)sheetView didSelectItemAtSection:(NSInteger)section index:(NSInteger)index {
    return;
}

@end
